package com.example.Android_test.activity.training;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.example.Android_test.R;
import com.example.Android_test.activity.Main;
import com.example.Android_test.helpers.SOWEngine;
import com.example.Android_test.helpers.SettingsHelper;
import com.example.Android_test.specs.WordSpec;
import com.example.Android_test.util.ReadyAnimations;

import java.util.ArrayList;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: dns
 * Date: 05.05.13
 * Time: 19:46
 * To change this template use File | Settings | File Templates.
 */
public class Training_ENG_to_RUS extends Abstract_Training {
    public static final String TITLE = "Training_ENG_to_RUS";
    public static int MAX_WORDS_IN_ONE_CYCLE = 5;

    private static int RIGHT_VARIANT_COLOR = Color.GREEN;
    private static int WRONG_VARIANT_COLOR = Color.RED;
    private static int ANOTHER_VARIANT_COLOR = Color.YELLOW;

    private Button etr_button_var1;
    private Button etr_button_var2;
    private Button etr_button_var3;
    private Button etr_button_var4;

    private Button etr_button_next;

    private TextView etr_textView_TrainingWord;
    private TextView etr_textView_context;
    private TextView etr_textView_contextTranslation;

    private ProgressBar etr_progressBar;

    private String rightTranslationVariant = null;
    private WordSpec trainingWord = null;

    private int goodCount;
    private int badCount;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.training_etr_menu, menu);
        return true;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.training_eng_to_rus);
        setTitle(TITLE);

        etr_progressBar = (ProgressBar) findViewById(R.id.etr_progressBar);
        etr_progressBar.setMax(MAX_WORDS_IN_ONE_CYCLE);
        etr_progressBar.setProgress(0);

        etr_button_var1 = (Button) findViewById(R.id.etr_var1);
        etr_button_var2 = (Button) findViewById(R.id.etr_var2);
        etr_button_var3 = (Button) findViewById(R.id.etr_var3);
        etr_button_var4 = (Button) findViewById(R.id.etr_var4);

        etr_button_var1.setOnClickListener(new VarButtonListener());
        etr_button_var2.setOnClickListener(new VarButtonListener());
        etr_button_var3.setOnClickListener(new VarButtonListener());
        etr_button_var4.setOnClickListener(new VarButtonListener());

        etr_button_next = (Button) findViewById(R.id.etr_buttonNext);
        etr_button_next.setOnClickListener(new NextButtonListener());

        etr_textView_TrainingWord = (TextView) findViewById(R.id.etr_textViewTrainingWord);
        etr_textView_context = (TextView) findViewById(R.id.etr_textViewContext);
        etr_textView_contextTranslation = (TextView) findViewById(R.id.etr_textViewContextTranslation);

        trainingStart();
    }

    @Override
    protected void onFirstRun() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected String getKeyTrainingTimeSpent() {
        return SettingsHelper.KEY_TRAINING_TIME_SPENT_ETR;
    }

    @Override
    protected void trainingStart() {
        super.trainingStart();

        goodCount = 0;
        badCount = 0;

        next();
    }

    @Override
    protected void trainingStop() {
        super.trainingStop();
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected void next() {
        prepareVariantButtons();

        etr_textView_context.setText("");
        etr_textView_contextTranslation.setText("");

        ReadyAnimations.animateView(this, etr_textView_TrainingWord, ReadyAnimations.ANIMATE_FADE_OUT);

        ReadyAnimations.animateViewWithDelay(this, etr_button_var1, ReadyAnimations.ANIMATE_FADE_OUT, 0);
        ReadyAnimations.animateViewWithDelay(this, etr_button_var2, ReadyAnimations.ANIMATE_FADE_OUT, 100);
        ReadyAnimations.animateViewWithDelay(this, etr_button_var3, ReadyAnimations.ANIMATE_FADE_OUT, 200);
        ReadyAnimations.animateViewWithDelay(this, etr_button_var4, ReadyAnimations.ANIMATE_FADE_OUT, 300);

        try {
            trainingWord = SOWEngine.INSTANCE.getWordToTraining();
        } catch (Exception e) {
            Log.e(Main.LOG_TAG, e.getMessage());
            return;
        }

        ArrayList<WordSpec> randWords;
        try {
            randWords = SOWEngine.INSTANCE.getRandomWords(trainingWord, 3);
        } catch (Exception e) {
            Log.e(Main.LOG_TAG, e.getMessage());
            return;
        }

        randWords.add(trainingWord);

        etr_textView_TrainingWord.setText(trainingWord.getWord());

        rightTranslationVariant = trainingWord.getTranslationVariantSpecs().get(0).getTranslate();

        Random rand = new Random();
        for (int i=0; i < 4; i++) {
            int randNum = rand.nextInt(randWords.size());

            switch (i) {
                case 0: etr_button_var1.setText(randWords.get(randNum).getTranslationVariantSpecs().get(0).getTranslate());
                    break;
                case 1: etr_button_var2.setText(randWords.get(randNum).getTranslationVariantSpecs().get(0).getTranslate());
                    break;
                case 2: etr_button_var3.setText(randWords.get(randNum).getTranslationVariantSpecs().get(0).getTranslate());
                    break;
                case 3: etr_button_var4.setText(randWords.get(randNum).getTranslationVariantSpecs().get(0).getTranslate());
                    break;
                default: break;
            }

            randWords.remove(randNum);
        }
    }

    private void prepareVariantButtons() {
        etr_button_var1.setBackgroundResource(R.drawable.button_custom);
        etr_button_var2.setBackgroundResource(R.drawable.button_custom);
        etr_button_var3.setBackgroundResource(R.drawable.button_custom);
        etr_button_var4.setBackgroundResource(R.drawable.button_custom);

        enableAllVariantButtons();
    }

    private void alertOnFinish() {
        String result = String.format(getResources().getString(R.string.training_etr_result), goodCount, goodCount+badCount);

        AlertDialog dialog = new AlertDialog.Builder(this).setMessage(result)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Toast.makeText(Main.getAppContext(), "PositiveButton", Toast.LENGTH_LONG).show();
                        dialog.dismiss();

                        etr_progressBar.setProgress(0);
                        trainingStart();
                    }
                }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Toast.makeText(Main.getAppContext(), "NegativeButton", Toast.LENGTH_LONG).show();
                        dialog.dismiss();

                        finish();
                    }
                }).setOnCancelListener(new DialogInterface.OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface dialog) {
                        //Toast.makeText(Main.getAppContext(), "onCancel", Toast.LENGTH_LONG).show();
                        dialog.dismiss();

                        finish();
                    }
                }).create();

        dialog.show();

        /*AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Reset...");
        alertDialog.setMessage("Are you sure?");
        //alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new AlertDialogButtonOKListener());
        // alertDialog.setIcon(R.drawable.icon);
        alertDialog.show();*/
    }

    private void enableAllVariantButtons() {
        etr_button_var1.setEnabled(true);
        etr_button_var2.setEnabled(true);
        etr_button_var3.setEnabled(true);
        etr_button_var4.setEnabled(true);
    }

    private void disableAllVariantButtons() {
        etr_button_var1.setEnabled(false);
        etr_button_var2.setEnabled(false);
        etr_button_var3.setEnabled(false);
        etr_button_var4.setEnabled(false);
    }

    private class VarButtonListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Button clickedButton = (Button) v;

            if (clickedButton.getText().equals(rightTranslationVariant)) {
                disableAllVariantButtons();

                clickedButton.setBackgroundColor(RIGHT_VARIANT_COLOR);
                trainingWord.setRating(trainingWord.getRating() + 1);
                trainingWord.setShowed(trainingWord.getShowed() + 1);

                playSound(trainingWord.getSoundResName());

                goodCount++;
            } else {
                clickedButton.setBackgroundColor(WRONG_VARIANT_COLOR);

                if (etr_button_var1.getText().equals(rightTranslationVariant)) etr_button_var1.setBackgroundColor(ANOTHER_VARIANT_COLOR);
                if (etr_button_var2.getText().equals(rightTranslationVariant)) etr_button_var2.setBackgroundColor(ANOTHER_VARIANT_COLOR);
                if (etr_button_var3.getText().equals(rightTranslationVariant)) etr_button_var3.setBackgroundColor(ANOTHER_VARIANT_COLOR);
                if (etr_button_var4.getText().equals(rightTranslationVariant)) etr_button_var4.setBackgroundColor(ANOTHER_VARIANT_COLOR);

                disableAllVariantButtons();

                trainingWord.setRating(trainingWord.getRating() - 1);

                badCount++;
            }

            etr_textView_context.setText(trainingWord.getInContext());
            etr_textView_contextTranslation.setText(trainingWord.getInContextTranslation());

            SOWEngine.INSTANCE.saveCurrentSetOfWords();

            etr_progressBar.setProgress(etr_progressBar.getProgress() + 1);
            if (etr_progressBar.getProgress() >= MAX_WORDS_IN_ONE_CYCLE) {
                alertOnFinish();

            } else {
                ReadyAnimations.animateView(getBaseContext(), etr_button_next, ReadyAnimations.ANIMATE_RIGHT_GO_GO);
            }
        }
    }

    private class NextButtonListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            next();
        }
    }
}